package com.yxj.dries.lib.network.request;

import java.io.File;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * @author dries
 * 对外提供get/post文件上传请求
 */
public class CommonRequest {

    /**
     * create the key-value Request
     */
    public static Request createPostRequest(String url, RequestParams params) {
        return createPostRequest(url, params, null);
    }

    /**
     * 可以带请求头的Post请求
     *
     * @param url
     * @param params
     * @param headers
     * @return
     */
    private static Request createPostRequest(String url, RequestParams params, RequestParams headers) {

        // 组装参数
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        if (params != null) {
            for (Map.Entry<String, String> entry : params.urlParams.entrySet()) {
                formBodyBuilder.add(entry.getKey(), entry.getValue());
            }
        }
        // 添加请求头
        Headers.Builder headersBuilder = new Headers.Builder();
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.urlParams.entrySet()) {
                headersBuilder.add(entry.getKey(), entry.getValue());
            }
        }

        FormBody formBody = formBodyBuilder.build();
        Headers mHeaders = headersBuilder.build();

        return new Request.Builder().url(url)
                .post(formBody)
                .headers(mHeaders)
                .build();
    }

    public static Request createGetRequest(String url, RequestParams params) {
        return createGetRequest(url, params, null);
    }

    /**
     * 可以带请求头的Get请求
     *
     * @param url
     * @param params
     * @param headers
     * @return
     */
    public static Request createGetRequest(String url, RequestParams params, RequestParams headers) {
        StringBuilder urlBuilder = new StringBuilder(url);

        if (url.indexOf("?") > 0 || url.indexOf("&") > 0) {
            urlBuilder.append("&");
        } else {
            urlBuilder.append("?");
        }

        if (params != null) {
            for (Map.Entry<String, String> entry : params.urlParams.entrySet()) {
                urlBuilder.append(entry.getKey())
                        .append("=")
                        .append(entry.getValue())
                        .append("&");
            }
        }
        //  添加请求头
        Headers.Builder headersBuilder = new Headers.Builder();
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.urlParams.entrySet()) {
                headersBuilder.add(entry.getKey(), entry.getValue());
            }
        }

        urlBuilder.deleteCharAt(urlBuilder.length() - 1);
        Headers mHeaders = headersBuilder.build();

        return new Request.Builder()
                .url(urlBuilder.toString())
                .headers(mHeaders)
                .build();
    }

    /**
     * 文件上传请求
     */
    private static final MediaType FILE_TYPE = MediaType.parse("application/octet-stream");

    public static Request createMultiPostRequest(String url, RequestParams params) {
        MultipartBody.Builder requestBody = new MultipartBody.Builder();
        requestBody.setType(MultipartBody.FORM);

        if (params != null) {
            for (Map.Entry<String, Object> entry : params.fileParams.entrySet()) {
                if (entry.getValue() instanceof File) {
                    requestBody.addPart(
                            Headers.of("Content-Disposition",
                                    "form-data; name=\"" + entry.getKey() + "\""),
                            RequestBody.create((File) entry.getValue(), FILE_TYPE));
                } else if (entry.getValue() instanceof String) {
                    // RequestBody.create(null, (String) entry.getValue())
                    requestBody.addPart(
                            Headers.of("Content-Disposition",
                                    "form-data; name=\"" + entry.getKey() + "\""),
                            RequestBody.create((String) entry.getValue(),
                                    null
                            ));
                }
            }
        }

        return new Request.Builder().url(url).post(requestBody.build()).build();
    }

}
